// 封装uni请求变成Promise风格
import { useAuthStore } from "@/stores/auth";
const authStore = useAuthStore();

interface IConfig {
	baseUrl : string;
	timeout ?: number;
	header ?: any;
}

interface IOptions {
	url : string;
	headers ?: any;
	method ?: "GET" | "POST" | "DELETE" | "PUT";
	data ?: any;
	showLoading ?: boolean;
}

interface IUploadOptions {
	url : string;
	headers ?: any;
	method ?: "GET" | "POST" | "DELETE" | "PUT";
	data : {
		name : string;
		filePath : any;
	};
}

export interface IReturnRes<T> {
	data ?: any;
	url ?: string;
	message ?: string;
	status : number;
	list ?: any;
}

export type IRes<T> = Promise<IReturnRes<T>>;

export default class Http {
	private readonly baseUrl : string;
	private readonly timeout : number;
	private readonly header : any;

	constructor(config : IConfig) {
		this.baseUrl = config.baseUrl || "";
		this.timeout = config.timeout || 6000;
		this.header = config.header || {};
	}

	_request<T = any>(options : IOptions) : Promise<IReturnRes<T>> {
		// showLoading
		if (!options.showLoading) {
			uni.showLoading({
				title: "加载中...",
				fail(result) {
					console.log(result);
				}
			});
		}
		return new Promise((resolve, reject) => {
			const token = authStore.getToken();
			uni.request({
				url: this.baseUrl + options.url,
				timeout: this.timeout,
				header: {
					// 请求头带token
					"Authorization": "Bearer " + token,
					...this.header,
					...options.headers // 可以请求时传自定义请求头
				},
				method: options.method || "GET",
				data: options.data,
				success: (res : any) => {
					const data : IReturnRes<T> = res.data;
					uni.hideLoading();
					if (data.status === 404) {
						uni.showToast({
							title: data.message,
							icon: "none",
							duration: 2000,
							fail(result) {
								console.log(result);
							}
						});
					} else if (data.status === 50000) {
						uni.showToast({
							title: data.message,
							icon: "error",
							fail(result) {
								console.log(result);
							},
							duration: 2000
						});
						// 需要登录权限
					} else if (data.status === 401) {
						uni.navigateTo({ url: "/pages/login/login" });
					}
					resolve(data);
				},
				fail: (err : any) => {
					uni.hideLoading();
					uni.showToast({
						title: err,
						icon: "error",
						fail(result) {
							console.log(result);
						}
					});
					reject(err);
				}
			});
		});
	}

	get<T = any>(options : IOptions) : Promise<IReturnRes<T>> {
		return this._request({
			...options
		});
	}

	post<T = any>(options : IOptions) : Promise<IReturnRes<T>> {
		return this._request({
			...options,
			method: "POST"
		});
	}

	put<T = any>(options : IOptions) : Promise<IReturnRes<T>> {
		return this._request({
			...options,
			method: "PUT"
		});
	}

	del<T = any>(options : IOptions) : Promise<IReturnRes<T>> {
		return this._request({
			...options,
			method: "DELETE"
		});
	}

	// 上传文件
	upload<T = any>(options : IUploadOptions) : Promise<IReturnRes<T>> {
		uni.showLoading({
			title: "上传中",
			fail(result) {
				console.log(result);
			}
		});
		return new Promise((resolve, reject) => {
			uni.uploadFile({
				url: this.baseUrl + options.url,
				header: {
					// 请求头带token
					"Authori-zation": "Bearer " + token,
					...options.headers
				},
				filePath: options.data.filePath,
				name: options.data.name,
				formData: {
					filename: options.data.name
				},
				success: (res : any) => {
					const data : IReturnRes<T> = res.data;
					uni.hideLoading();
					if (data.status != 0) {
						uni.showToast({
							title: data.message,
							icon: "none",
							fail(result) {
								console.log(result);
							}
						});
					}
					resolve(data);
				},
				fail: (err : any) => {
					uni.hideLoading();
					uni.showToast({
						title: "请求超时",
						icon: "error",
						fail(result) {
							console.log(result);
						}
					});
					reject(err);
				}
			});
		});
	}
}